from rest_framework.permissions import BasePermission

from apps.Blog.Errors import UserPermissionException
from apps.Blog_User.models import UserTokenModel


class CategoryPermission(BasePermission):
    def has_permission(self, request, view):
        return True

    def has_object_permission(self, request, view, obj):
        _user_info = UserTokenModel.get_user_token(user_token=request.query_params.get("token", None))
        if request.method != 'GET' and obj.author.id != _user_info.user.id:
            raise UserPermissionException
        return True


class TagPermission(BasePermission):
    def has_permission(self, request, view):
        return True

    def has_object_permission(self, request, view, obj):
        _user_info = UserTokenModel.get_user_token(user_token=request.query_params.get("token", None))
        if request.method != 'GET' and obj.author.id != _user_info.user.id:
            raise UserPermissionException
        return True


class ArticlePermission(BasePermission):
    def has_permission(self, request, view):
        return True

    def has_object_permission(self, request, view, obj):
        _user_info = UserTokenModel.get_user_token(user_token=request.query_params.get("token", None))
        if request.method != 'GET' and obj.author.id != _user_info.user.id:
            raise UserPermissionException
        return True
